import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { FormsModule, Validators, FormGroup, FormControl } from '@angular/forms';
import * as moment from 'moment';
import * as _ from 'lodash';
import { DataService } from '../../../services/data.service';
import { FileUploader } from "ng2-file-upload";
import { environment } from '../../../../environments/environment';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { OnetomanyEnclosureService } from "./enclosure.service";
import { Util } from 'app/common/util';

@Component({
  selector: 'app-enclosure',
  templateUrl: './enclosure.component.html',
  styleUrls: ['./enclosure.component.scss'],
  providers: [OnetomanyEnclosureService]
})
export class EnclosureComponent implements OnInit {
  // lodash
  public _: any = _;
  // 获取权限按钮
  public menuResourceSet: any;

  public menuData: any;
  // 列表是否全选
  public isCheckedAll = false;
  //  托管人数据
  // public bankDictionaryList = [];
  // 一对多附件列表数据
  public otmAttachList: any;
  // 一对多附件类型查询条件
  public otmAttachConf: any;
  // 附件子类型
  public fileChildType;
  // 一对多附件
  public otmAttachBody: any = {
    sFundType: "2",
    sFundCode: "",
    sFullName: "",
    sAttachCode: "",
    sAttachName: "",
    sAttachParentCode: "",
    pageIndex: 1,
    pageSize: 10,
    sTrustee: ""
  }
  // 一对多附件列表分页
  public otmAttachListPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };

  constructor(
    public otmEnclosureService: OnetomanyEnclosureService,
    public dataservice: DataService
  ) { }

  ngOnInit() {
    this.getAttachConf();
    this.getAttachList();

    // 权限处理
    this.dataservice.initTree.subscribe((data) => {
      let SessionMenuData = sessionStorage.getItem('menu');
      this.menuData = JSON.parse(SessionMenuData)
      this.menuResourceSet = [];
      _.forEach(this.menuData, (item, index) => {
        if (item.sName == '一对多专户') {
          _.forEach(item.children, (it, idx) => {
            if (it.sName == '附件查询') {
              this.menuResourceSet = it.resourceSet
            }

          })
        }
      })
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData)
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '一对多专户') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '附件查询') {
            this.menuResourceSet = it.resourceSet
          }

        })
      }
    })
    // 获取托管人数据
    // this.bankDictionaryList = JSON.parse(localStorage.getItem("bankDictionaryList"));
  }
  // 一对多附件列表查询
  getAttachList(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.otmAttachBody.sFullName = _.trim(this.otmAttachBody.sFullName);
    this.otmAttachBody.sFundCode = _.trim(this.otmAttachBody.sFundCode);
    this.otmAttachBody.sAttachName = _.trim(this.otmAttachBody.sAttachName);
    this.otmAttachBody.pageIndex = pageNum ? pageNum : this.otmAttachListPageInfo.currentPageNum;
    this.otmAttachBody.pageSize = pageSize ? pageSize : this.otmAttachListPageInfo.pageSize;
    this.otmEnclosureService.getAttachList(this.otmAttachBody).subscribe(data => {
      if (data) {
        that.otmAttachList = data.list;
        that.otmAttachListPageInfo.currentPageNum = data.pageNum;
        that.otmAttachListPageInfo.totalPages = data.pages;
        that.otmAttachListPageInfo.total = data.total;
        that.otmAttachListPageInfo.startRow = data.startRow;
        that.otmAttachListPageInfo.endRow = data.endRow;
        setTimeout(() => {
          _.forEach(that.otmAttachList, it => {
            it['attachParentType'] = it.sAttachParentType;
          });
        }, 100)
      }
    })
  }
  // 一对多附件列表切换页
  otmAttachPagesizeChange(pageSize: number) {
    if (pageSize !== this.otmAttachListPageInfo.pageSize) {
      this.otmAttachListPageInfo.pageSize = pageSize;
      this.getAttachList();
    }
  }
  // 一对多附件列表分页
  otmAttachPageNavigation(currentPageNum: number) {
    this.otmAttachListPageInfo.currentPageNum = currentPageNum;
    this.getAttachList(this.otmAttachListPageInfo.currentPageNum, this.otmAttachListPageInfo.pageSize);
  }
  // 附件类型查询条件
  getAttachConf() {
    let that = this;
    this.otmEnclosureService.getAttachConf().subscribe(data => {
      that.otmAttachConf = data[0].childrenAttachType;
    })
  }
  // 全选
  checkedAll() {
    if (this.isCheckedAll) { // 更新为全选
      _.forEach(this.otmAttachList, item => {
        item.isChecked = true;
      });
    } else { // 更新为不全选
      _.forEach(this.otmAttachList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.otmAttachList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.otmAttachList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }

  // 下载附件
  attachDownload() {
    // 获取下载附件的内码
    let checkedItem = _.concat([], _.map(_.filter(this.otmAttachList, { isChecked: true }), "nAttachNo"));

    if (checkedItem.length == 0) {
      window['swal']("提示", "请选择附件再下载 !", "warning")
    } else {
      // 下载附件
      Util.attachDownload(checkedItem);
    }
  }
  //  获取附件子类型
  getChildType(value) {
    if (value != '') {
      let allType;
      allType = this.otmAttachConf.find((item) => {
        return item.sAttachTypeCode == value
      })
      this.fileChildType = allType.childrenAttachType;
      this.otmAttachBody.sAttachCode = ''
    } else {
      this.otmAttachBody.sAttachCode = '';
      this.fileChildType = [];
    }
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.otmAttachList, [colName], ["asc"]);
    if (JSON.stringify(this.otmAttachList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.otmAttachList, [colName], ["desc"]);
    }
    this.otmAttachList = listDataAsc;
  }
}
